What is resolve-url?
The resolve-url package is a utility for resolving URLs based on a base URL. It is commonly used to resolve relative paths to absolute URLs in the context of web development.
What are resolve-url's main functionalities?
Resolve relative URL
This feature allows you to resolve a relative URL to an absolute URL using a base URL. In the code sample, '/some/path' is resolved against the base URL 'http://example.com/' to form the absolute URL 'http://example.com/some/path'.
var resolve = require('resolve-url');
var resolvedUrl = resolve('http://example.com/', '/some/path');
Resolve URL with query string
This feature enables resolving a relative URL in the presence of a query string in the base URL. The code sample demonstrates how 'path' is resolved against 'http://example.com/?query=123' to form 'http://example.com/path?query=123'.
var resolve = require('resolve-url');
var resolvedUrl = resolve('http://example.com/?query=123', 'path');
Resolve URL with hash fragment
This feature is used to resolve a relative URL when there is a hash fragment in the base URL. In the code sample, 'path' is resolved against 'http://example.com/#hash' to form 'http://example.com/path#hash'.
var resolve = require('resolve-url');
var resolvedUrl = resolve('http://example.com/#hash', 'path');
Other packages similar to resolve-url
url-resolve-browser
This package provides similar functionality to resolve-url but is specifically designed for browser environments. It resolves relative URLs against a base URL, similar to resolve-url, but may have differences in handling edge cases or browser-specific quirks.
url-join
url-join is a package that allows for joining multiple URL segments together, ensuring that there are no duplicate slashes and that the segments are properly concatenated. It differs from resolve-url in that it focuses on joining segments rather than resolving relative URLs against a base URL.
url-parse
url-parse is a robust URL parsing library that can handle parsing URLs, resolving them, and normalizing them. It offers more comprehensive URL manipulation capabilities compared to resolve-url, which is more focused on resolving URLs.
Overview
Like Node.js’ path.resolve
/url.resolve
for the browser.
var resolveUrl = require("resolve-url")
window.location
resolveUrl("remove")
resolveUrl("/static/scripts/app.js")
resolveUrl("/static/scripts/app.js", "../source-maps/app.js.map")
resolveUrl("/static/scripts/app.js", "../source-maps/app.js.map", "../coffee/app.coffee")
resolveUrl("//cdn.example.com/jquery.js")
resolveUrl("http://foo.org/")
Installation
npm install resolve-url
bower install resolve-url
component install lydell/resolve-url
Works with CommonJS, AMD and browser globals, through UMD.
Usage
resolveUrl(...urls)
Pass one or more urls. Resolves the last one to an absolute url, using the
previous ones and window.location
.
It’s like starting out on window.location
, and then clicking links with the
urls as href
attributes in order, from left to right.
Unlike Node.js’ path.resolve
, this function always goes through all of the
arguments, from left to right. path.resolve
goes from right to left and only
in the worst case goes through them all. Should that matter.
Actually, the function is really like clicking a lot of links in series: An
actual <a>
gets its href
attribute set for each url! This means that the
url resolution of the browser is used, which makes this module really
light-weight.
Also note that this functions deals with urls, not paths, so in that respect it
has more in common with Node.js’ url.resolve
. But the arguments are more
like path.resolve
.
Tests
Run npm test
, which lints the code and then gives you a link to open in a
browser of choice (using testling
).
License
The X11 (“MIT”) License.